package com.atguigu.blog.controller;

import com.atguigu.blog.constant.SessionConstant;
import com.atguigu.blog.pojos.Article;
import com.atguigu.blog.pojos.User;
import com.atguigu.blog.service.ArticleService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CF
 * @date 2023/8/19 9:40
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping("/queryList")
    public List<Article> queryList(@RequestBody Article article){
        return articleService.queryList(article.getCid());
    }
    @GetMapping("/getArticle/{id}")
    public Article getArticle(@PathVariable("id") Integer id){
        return  articleService.findArticleById(id);
    }

    @PostMapping("/addArticle")
    public boolean addArticle(@RequestBody Article article, HttpSession session){
        //1.从session中获取登录成功的用户信息
        User loginUser= (User) session.getAttribute(SessionConstant.LOGIN_SESSION_KEY);
        //2.给article设置uid
        if (loginUser !=null) {
        article.setUid(loginUser.getUid());
        }
        //3.调用业务层方法添加文章
        return articleService.addArticle(article);
    }

    @DeleteMapping("/deleteArticle/{id}")
    public boolean deleteArticle(@PathVariable("id") Integer id){
        return articleService.deleteArticleById(id);
    }
}
